home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM 1995 Fall / PD-ROM F95.toast / Programming / Programming Languages / UCB Logo 3.0 ƒ / sources / standard source / macterm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-14  |  11.7 KB  |  506 lines  |  [TEXT/ttxt]

  1. /*
  2.  *      macterm.c          macintosh screen module          mak
  3.  *
  4.  *    Copyright (C) 1993 by the Regents of the University of California
  5.  *
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 2 of the License, or
  9.  *      (at your option) any later version.
  10.  *  
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *  
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, write to the Free Software
  18.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  */
  21.  
  22. #include "logo.h"
  23. #include "globals.h"
  24. #include "macterm.h"
  25. #include <console.h>
  26. #include <math.h>
  27. #include <Sound.h>
  28.  
  29. char windowtitle[100];
  30. FILE *graphics, *console;
  31. WindowPtr graphics_window, listener_window;
  32. GrafPtr savePort;
  33. extern WindowPtr myWindow; /* the editor window */
  34. FIXNUM pen_color = 7, back_ground = 0;
  35. extern void save_color();
  36.  
  37. /************************************************************/
  38.  
  39. void nop()
  40. {
  41. }
  42.  
  43. void init_mac_memory()
  44. {
  45.     unsigned long AZ, AL;
  46.     /* SetApplLimit((Ptr)(GetApplLimit() - 150000L)); MaxApplZone(); */
  47.     
  48.     AL = (unsigned long)GetApplLimit(); AZ = (unsigned long)ApplicZone();
  49. /*    SetApplLimit((Ptr) ( (AZ + ((AL-AZ)*3)/4) & 0x00fffffe )); */
  50.         /* SetApplLimit((Ptr)(AZ + 300000L)); */
  51.     SetApplLimit((Ptr)((AZ + ((AL-AZ)*3)/4) & -2L)); /* PCB */
  52.     MaxApplZone();
  53. }
  54.  
  55. BOOLEAN check_mac_stop()
  56. {
  57.     unsigned long   theStackPointer;
  58.     unsigned long   AL;
  59.     char        the_key_map[16];
  60.     
  61.     asm {
  62.     Move.L  SP,theStackPointer
  63.     }
  64. /*    theStackPointer = theStackPointer & 0x00ffffff; */
  65.     AL = (unsigned long)(GetApplLimit());
  66.     AL = AL + 5000;
  67. /*    AL = AL & 0x00ffffff; */
  68.     if (theStackPointer < AL || FreeMem() < 3000) {
  69.     err_logo(STACK_OVERFLOW, NIL);
  70.     return(1);
  71.     }
  72.     GetKeys(&the_key_map);
  73.     if (the_key_map[5] & 128 && the_key_map[6] & 128) {
  74.             /* period and command are down */
  75.     FlushEvents(everyEvent, 0);
  76.     logo_stop();
  77.     } else if (the_key_map[5] & 8 && the_key_map[6] & 128) {
  78.             /* comma and command are down */
  79.     FlushEvents(everyEvent, 0);
  80.     logo_pause();
  81.     }
  82.     return(0);
  83. }
  84.  
  85. void term_init_mac()
  86. {
  87.     MenuHandle menu_handle;
  88.     
  89.     tty_charmode = 0;
  90.     x_max = 80;
  91.     y_max = 24;
  92.     console_options.title = windowtitle;
  93.     console_options.top-= 5;
  94.     console_options.left-= 5;
  95.     strncpy(console_options.title, "\pGraphics", 9);
  96.     graphics = fopenc();
  97.     cgotoxy(1, 1, graphics);
  98.     graphics_window = FrontWindow();
  99.     SizeWindow(graphics_window, graphics_window->portRect.right,
  100.                 graphics_window->portRect.bottom - 1, 0);
  101.     cs_helper(TRUE);
  102.  
  103.     console_options.top+= 10;
  104.     console_options.left+= 10;
  105.     strncpy(console_options.title, "\pBerkeley Logo", 14);
  106.     console = fopenc();
  107.     lregulartext();
  108.     cinverse(1,stdout);
  109.     listener_window = FrontWindow();
  110.     
  111.     console_options.title[0] = 0;
  112.     
  113.     menu_handle = GetMHandle(3);
  114.     AppendMenu(menu_handle,
  115.            "\p(-;(Accept Editor Changes/A;(Cancel Editor Changes");
  116.     AppendMenu(menu_handle, "\p(-;(Page Setup;(Print/P");
  117.     SetUpWindows();
  118.     prepare_to_draw;
  119.     mac_set_bg(0L);
  120.     mac_set_pc(7L);
  121.     save_color();
  122.     done_drawing;
  123.  
  124.     x_coord = y_coord = 0;
  125.     so_arr[0] = '\1'; so_arr[1] = '\0';
  126.     se_arr[0] = '\2'; se_arr[1] = '\0';
  127. }
  128.  
  129. void mac_gotoxy(int x, int y)
  130. {
  131.     if (x_coord < 0) x_coord = 0;
  132.     if (x_coord >= console_options.ncols) x_coord = console_options.ncols - 1;
  133.     if (y_coord < 0) y_coord = 0;
  134.     if (y_coord >= console_options.nrows) y_coord = console_options.nrows - 1;
  135.     cgotoxy(x_coord + 1, y_coord + 1, stdout);
  136. }
  137.  
  138. /************************************************************/
  139. /* These are primitives that can only exist on the mac and/or are ad hoc
  140.    things Michael invented that we probably don't want to keep around in Berkeley Logo. */
  141.  
  142. NODE *lsetwindowtitle(arg)
  143. NODE *arg;
  144. {
  145.     NODE *name;
  146.     
  147.     name = string_arg(arg);
  148.     if (name != UNBOUND) {
  149.     noparity_strnzcpy((char *)(windowtitle + 1), getstrptr(name), (int)getstrlen(name));
  150.     windowtitle[0] = (char)getstrlen(name);
  151.     }
  152.     return(UNBOUND);
  153. }
  154.  
  155. option_helper(var, arg)
  156. short *var;
  157. NODE *arg;
  158. {
  159.     NODE *val;
  160.     
  161.     val = integer_arg(arg);
  162.     if (NOT_THROWING)
  163.     *var = (short)getint(val);
  164. }
  165.  
  166. NODE *lsettextfont(arg)
  167. NODE *arg;
  168. {
  169.     option_helper(&console_options.txFont, arg);
  170.     return(UNBOUND);
  171. }
  172.  
  173. NODE *lsettextsize(arg)
  174. NODE *arg;
  175. {
  176.     option_helper(&console_options.txSize, arg);
  177.     return(UNBOUND);
  178. }
  179.  
  180. NODE *lsettextstyle(arg)
  181. NODE *arg;
  182. {
  183.     option_helper(&console_options.txFace, arg);
  184.     return(UNBOUND);
  185. }
  186.  
  187. NODE *lsetwindowsize(args)
  188. NODE *args;
  189. {
  190.     NODE *xnode, *ynode = UNBOUND, *arg;
  191.  
  192.     arg = pos_int_vector_arg(args);
  193.     if (NOT_THROWING) {
  194.     xnode = car(arg);
  195.     ynode = cadr(arg);
  196.     console_options.ncols =  max((int)getint(xnode), 40);
  197.     console_options.nrows =  max((int)getint(ynode), 5);
  198.     }
  199.     return(UNBOUND);
  200. }
  201.  
  202. NODE *lsetwindowxy(args)
  203. NODE *args;
  204. {
  205.     NODE *xnode, *ynode = UNBOUND, *arg;
  206.  
  207.     arg = pos_int_vector_arg(args);
  208.     if (NOT_THROWING) {
  209.     xnode = car(arg);
  210.     ynode = cadr(arg);
  211.     console_options.left = (int)getint(xnode);
  212.     console_options.top = (int)getint(ynode);
  213.     }
  214.     return(UNBOUND);
  215. }
  216.  
  217. NODE *lnewconsole()
  218. {
  219.     FILE *c, *old;
  220.     int was_graphics;
  221.     
  222.     was_graphics = (FrontWindow() == graphics_window);
  223.     
  224.     chide(stdin);
  225.     fclose(stdin);
  226.     fclose(stdout);
  227.     fclose(stderr);
  228.     c = fopenc();
  229.     fclose(stdin);
  230.     fclose(stdout);
  231.     freopenc(c, stdin);
  232.     freopenc(c, stdout);
  233.     freopenc(c, stderr);
  234.     lcleartext();
  235.     cinverse(1,stdout);
  236.     
  237.     if (was_graphics) {
  238.     graphics_window = FrontWindow();
  239.     graphics = c;
  240.     }
  241.     else
  242.     console = c;
  243.     return(UNBOUND);
  244. }
  245.  
  246. NODE *lgraphtext()
  247. {
  248.     freopenc(graphics, stdin);
  249.     freopenc(graphics, stdout);
  250.     freopenc(graphics, stderr);
  251.     return(UNBOUND);
  252. }
  253.  
  254. NODE *lregulartext()
  255. {
  256.     freopenc(console, stdin);
  257.     freopenc(console, stdout);
  258.     freopenc(console, stderr);
  259.     return(UNBOUND);
  260. }
  261.  
  262. /************************************************************/
  263. /* These are the machine-specific graphics definitions.  All versions must provide
  264.    a set of functions analogous to these. */
  265.  
  266. void save_pen(pen_info *p)
  267. {
  268.     GetPort(&savePort);
  269.     SetPort(graphics_window);
  270.     GetPenState(&(p->ps));
  271.     p->vis = graphics_window->pnVis;
  272.     p->color = graphics_window->fgColor;
  273.     SetPort(savePort);
  274. }
  275.  
  276. void restore_pen(pen_info *p)
  277. {
  278.     GetPort(&savePort);
  279.     SetPort(graphics_window);
  280.     SetPenState(&(p->ps));
  281.     graphics_window->pnVis = p->vis;
  282.     graphics_window->fgColor = p->color;
  283.     SetPort(savePort);
  284. }
  285.  
  286. void plain_xor_pen()
  287. {
  288.     PenNormal();
  289.     PenMode(patXor);
  290. }
  291.  
  292. FIXNUM color_table[8] = {33, 409, 341, 273, 205, 137, 69, 30};
  293.  
  294. FIXNUM hw_color(FIXNUM c) {
  295.     if (c >= 0 && c < 8) return color_table[c];
  296.     if (c < 0) return c;
  297.     return c-8;
  298. }
  299.  
  300. void mac_set_pc(FIXNUM c) {
  301.     pen_color = c;
  302.     graphics_window->fgColor = hw_color(c);
  303. }
  304.  
  305. void mac_set_bg(FIXNUM c) {
  306.     back_ground = c;
  307.     graphics_window->bkColor = hw_color(c);
  308.     redraw_graphics();
  309. }
  310.  
  311. void set_pen_pattern(char *pat)
  312. {
  313.     int count;
  314.     
  315.     for (count = 0; count < 8; count++)
  316.     graphics_window->pnPat[count] = *(char *)(pat + count);
  317. }
  318.  
  319. void set_list_pen_pattern(NODE *arg)
  320. {
  321.     NODE *cur_num, *temp;
  322.     char *p_arr;
  323.     int count;
  324.  
  325.     p_arr= &graphics_window->pnPat;
  326.     cur_num = arg;
  327.     for (count = 0 ; count <= 7 ; count++) {
  328.     temp = cnv_node_to_numnode(car(cur_num));
  329.     p_arr[count] = (char)getint(temp);
  330.     if (cdr(cur_num) != NIL)
  331.         cur_num = cdr(cur_num);
  332.     gcref(temp);
  333.     }
  334. }
  335.  
  336. void get_pen_pattern(char *pat)
  337. {
  338.     int count;
  339.  
  340.     for (count = 0; count < 8; count++)
  341.      *(char *)(pat + count) = graphics_window->pnPat[count];
  342. }
  343.  
  344. NODE *Get_node_pen_pattern()
  345. {
  346.     return(cons(make_intnode((FIXNUM)(graphics_window->pnPat[0])),
  347.         cons(make_intnode((FIXNUM)(graphics_window->pnPat[1])),
  348.          cons(make_intnode((FIXNUM)(graphics_window->pnPat[2])),
  349.           cons(make_intnode((FIXNUM)(graphics_window->pnPat[3])),
  350.            cons(make_intnode((FIXNUM)(graphics_window->pnPat[4])),
  351.         cons(make_intnode((FIXNUM)(graphics_window->pnPat[5])),
  352.          cons(make_intnode((FIXNUM)(graphics_window->pnPat[6])),
  353.           cons(make_intnode((FIXNUM)(graphics_window->pnPat[7])),
  354.            NIL)))))))));
  355. }
  356.  
  357. NODE *Get_node_pen_mode()
  358. {
  359.     switch(pen_mode) {
  360.     case patCopy   : return(make_static_strnode("paint"));
  361.     case patBic    : return(make_static_strnode("erase"));
  362.     case patXor    : return(make_static_strnode("reverse"));
  363.     default           : return(make_static_strnode("unknown"));
  364.     }
  365. }
  366.  
  367. void label(char *s)
  368. {
  369.     GetPort(&savePort);
  370.     SetPort(graphics_window);
  371.     MoveTo(g_round(graphics_window->portRect.right/2.0 + turtle_x),
  372.        g_round(graphics_window->portRect.bottom/2.0 - turtle_y));
  373.     TextFont(monaco); TextSize(9); TextMode(srcOr);
  374.     DrawString(s);
  375.     SetPort(savePort);
  376. }
  377.  
  378. void logofill()    /* too bad mac toolbox doesn't include FloodFill() */
  379. {
  380. }
  381.  
  382. void erase_screen()
  383. {
  384.     int old_vis;
  385.     
  386.     GetPort(&savePort);
  387.     SetPort(graphics_window);
  388.     old_vis = graphics_window->pnVis;
  389.     graphics_window->pnVis = 0;
  390.     EraseRect(&graphics_window->portRect);
  391.     graphics_window->pnVis = old_vis;
  392.     SetPort(savePort);
  393. }
  394.  
  395. void t_screen()
  396. {
  397.     console_options.ncols = 80;
  398.     console_options.nrows = 25;
  399.     console_options.left = 15;
  400.     console_options.top = 55;
  401.     strncpy(console_options.title, "\pBerkeley Logo", 14);
  402.     lnewconsole();
  403.  
  404.     MoveWindow(myWindow, 15, 55, TRUE);
  405.     MySizeWindow(myWindow, 488, 283);
  406. }
  407.  
  408. void s_screen()
  409. {
  410.     Rect bounds;
  411.     int v;
  412.     
  413.     v = (graphics_window->portBits.bounds.bottom -
  414.         graphics_window->portBits.bounds.top) - 84;
  415.  
  416.     console_options.ncols = 80;
  417.     console_options.nrows = 6;
  418.     console_options.left = 5;
  419.     console_options.top = v + 6;
  420.     strncpy(console_options.title, "\pBerkeley Logo", 14);
  421.     lnewconsole();
  422.  
  423.     MoveWindow(myWindow, 5, v, TRUE);
  424.     MySizeWindow(myWindow, 488, 80);
  425. }
  426.  
  427. void f_screen()
  428. {
  429.     Rect bounds;
  430.     int v;
  431.     
  432.     v = (graphics_window->portBits.bounds.bottom -
  433.         graphics_window->portBits.bounds.top) - 84;
  434.  
  435.     console_options.ncols = 80;
  436.     console_options.nrows = 0;
  437.     console_options.left = 5;
  438.     console_options.top = v + 52;
  439.     strncpy(console_options.title, "\pBerkeley Logo", 14);
  440.     lnewconsole();
  441.  
  442.     MoveWindow(myWindow, 5, v, TRUE);
  443.     MySizeWindow(myWindow, 488, 80);
  444.     SelectWindow(graphics_window);
  445. }
  446.  
  447. FIXNUM mickey_x()
  448. {
  449.     Point the_mouse;
  450.     
  451.     GetPort(&savePort);
  452.     SetPort(graphics_window);
  453.     GetMouse(&the_mouse);
  454.     SetPort(savePort);
  455.     
  456.     return((FIXNUM)(the_mouse.h - graphics_window->portRect.right/2));
  457. }
  458.  
  459. FIXNUM mickey_y()
  460. {
  461.     Point the_mouse;
  462.     
  463.     GetPort(&savePort);
  464.     SetPort(graphics_window);
  465.     GetMouse(&the_mouse);
  466.     SetPort(savePort);
  467.     
  468.     return((FIXNUM)(graphics_window->portRect.bottom/2 - the_mouse.v));
  469. }
  470.  
  471. /* see Inside Macintosh vol. 2 pp. 237-241 for pitch values */
  472. void tone(pitch, duration)
  473. FIXNUM pitch, duration;
  474. {
  475.     struct { int mode;
  476.          int freq;
  477.          int amp;
  478.          int dur;
  479.        } sound_rec;
  480.     
  481.     sound_rec.mode = -1;
  482.     sound_rec.freq = (int)(387205L/pitch);
  483.     sound_rec.amp = 200;
  484.     sound_rec.dur = (int)duration;
  485.     
  486.     StartSound(&sound_rec, (long)8, (char *)(-1));
  487.     while (!SoundDone()) ;
  488. }
  489.  
  490. /************************************************************/
  491.  
  492. void c_to_pascal_string(str, len)
  493. char *str;
  494. int len;
  495. {
  496.     int count = len;
  497.     char prev;
  498.     
  499.     while (count > 0) {
  500.     prev = str[count - 1];
  501.     str[count] = clearparity(prev);
  502.     count--;
  503.     }
  504.     str[0] = len;
  505. }
  506.